import { submitDealReq } from './auth'
import { getBtnOperator } from './hylist'
import { getRemoteUrlParam, getRemoteUrlDataSetKey, getCommonRemoteUrlParam, getCommonRemoteUrlVerify, getTableSelectRow } from '@/utils/hyurl'
import { getGlobalParam } from './hyrecord'

export function fetchTreeData(thisObj, node, resolve) {
  let record = {}
  if (node && node !== null) {
    thisObj.loading = true
    record = node.dataRef
    thisObj.globalData.clickRow = record
    thisObj.globalData.globalParam = getGlobalParam(thisObj)
  }
  const btnItem = thisObj.globalData.clickItem
  let urlKey = 'list'
  if (btnItem !== null) {
    const operator = getBtnOperator(thisObj, thisObj.globalData, btnItem)
    if (operator !== undefined && operator !== null) {
      urlKey = operator.s_REF_URL_KEY
    }
  }
  const postParam = getRemoteUrlParam(thisObj, urlKey, thisObj.globalData)
  const verify = getCommonRemoteUrlVerify(thisObj, urlKey, thisObj.globalData)
  if (verify === 1) {
    // 查询验证未通过
    return
  }
  if (postParam === null || postParam.S_PAGE_ID === '') {
    return
  }
  thisObj.loading = true
  thisObj.spinning = true
  return submitDealReq(postParam).then((res) => {
    thisObj.loading = false
    thisObj.spinning = false
    if (res.data.success) {
      if (node && node !== null) {
        node.dataRef.children = res.data.data
        thisObj.tableData = [...thisObj.tableData]
      } else {
        thisObj.expandedKeys = []
        thisObj.tableData = res.data.data
      }
    } else {
      thisObj.$message.warning(res.data.message)
    }
  }).catch(reason => {
    console.log('异常信息', reason)
  })
}
